package com.azhon.basic.storage.sqlite;

import android.content.ContentValues;

import com.blankj.utilcode.util.LogUtils;


import org.litepal.crud.DataSupport;

import java.util.List;

import androidx.annotation.NonNull;

public class UserHistoryOperation {
    
    @NonNull
    private static UserHistoryOperation op = new UserHistoryOperation();
    private UserHistoryOperation(){}
    
    @NonNull
    public static UserHistoryOperation getUserHistoryOperation(){
        return op;
    }
    
    /**
     * 存储历史记录
     * @param functionId
     * @param useName
     * @return
     */
    public boolean saveUserHistory(String functionId, String useName, int resId, String num, String loginUserName){
        UseHistory lUseHistory = new UseHistory();
        lUseHistory.setFunctionId(functionId);
        lUseHistory.setUseName(useName);
        lUseHistory.setResId(resId);
        lUseHistory.setTimeStamp(System.currentTimeMillis());
        lUseHistory.setNum(num);
        lUseHistory.setLoginUserName(loginUserName);
        return lUseHistory.save();
    }
    
    /**
     * 插入或更新用户时间戳
     * @param functionId
     * @param useName
     */
    public void insertOrUpdateUserHistoryTimeStamp(String functionId, String useName, int resId, String num, String loginUserName) {
        List<UseHistory> lUseHistoryList = DataSupport.where("functionId = ? and useName = ? and loginUserName = ?", functionId, useName, loginUserName).find(UseHistory.class);
        LogUtils.e(lUseHistoryList);
        if (lUseHistoryList.isEmpty()){
            UserHistoryOperation.getUserHistoryOperation().saveUserHistory(functionId, useName, resId, num, loginUserName);
        }else{
            ContentValues values = new ContentValues();
            values.put("timeStamp", System.currentTimeMillis());
            DataSupport.updateAll(UseHistory.class, values, "functionId = ? and useName = ? and loginUserName = ?", functionId, useName, loginUserName);
        }
    }
    
    /**
     * 是否有历史记录
     */
    public boolean hasHistoryRecord(String loginUserName) {
        List<UseHistory> lUseHistoryList = DataSupport.where("loginUserName = ?", loginUserName).find(UseHistory.class);
//        LogUtils.e(lUseHistoryList);
        if (lUseHistoryList.isEmpty()){
            return false;
        }else{
            return true;
        }
    }
    
    /**
     * 历史记录条数
     */
    public int numberOfHistoryRecord(String loginUserName) {
        List<UseHistory> lUseHistoryList = DataSupport.limit(12).order("timestamp DESC").where("loginUserName = ?", loginUserName).find(UseHistory.class);
//        LogUtils.e(lUseHistoryList);
        if (!lUseHistoryList.isEmpty()){
            return lUseHistoryList.size();
        }else{
            return 0;
        }
    }
    
    /**
     * 当前用户最近几条历史记录（最多12条）
     */
    public List<UseHistory> DateOfHistoryRecord(String loginUserName) {
        List<UseHistory> lUseHistoryList = DataSupport.limit(12).order("timestamp DESC").where("loginUserName = ?", loginUserName).find(UseHistory.class);
        return lUseHistoryList;
    }

    public void deleteUseHistory(){
        UseHistory.deleteAll(UseHistory.class);
    }

    public void deleteLoginUserUseHistory(String loginUserName){
        UseHistory.deleteAll(UseHistory.class, "loginUserName = ?", loginUserName);
    }

}
